#ifndef USBUVC_H
#define USBUVC_H
#include "mmpf_typedef.h"
#include "mmpf_usbpccam.h"
#include "mmpf_usb_h264.h"
#include "mmpf_dma.h"
#include "mmpf_fctl.h"

#if 0
typedef struct
{
    MMP_USHORT bmHint;
    MMP_USHORT wIndex; //H byte -> Format Index, L Byte -> Frame Index 
    MMP_USHORT wIntervalLo;
    MMP_USHORT wIntervalHi;
    MMP_USHORT wKeyFrameRate;
    MMP_USHORT wPFrameRate;
    MMP_USHORT wCompQuality;
    MMP_USHORT wCompWinSize;
    MMP_USHORT wDelay;
    MMP_USHORT wMaxFrameSizeLo;
    MMP_USHORT wMaxFrameSizeHi;    
    MMP_USHORT wMaxPayloadSizeLo;
    MMP_USHORT wMaxPayloadSizeHi;
} VIDEO_PROBE;
#else
typedef __packed struct
{
    MMP_USHORT bmHint;
    //MMP_USHORT wIndex; //H byte -> Format Index, L Byte -> Frame Index
    MMP_UBYTE  bFormatIndex;
    MMP_UBYTE  bFrameIndex;
    MMP_ULONG  dwInterval;
    MMP_USHORT wKeyFrameRate;
    MMP_USHORT wPFrameRate;
    MMP_USHORT wCompQuality;
    MMP_USHORT wCompWinSize;
    MMP_USHORT wDelay;
    MMP_ULONG  dwMaxFrameSize;
    MMP_ULONG  dwMaxPayloadSize;
} VIDEO_PROBE;
#endif

#if 1
typedef __packed struct
{
    MMP_UBYTE bFormatIndex;
    MMP_UBYTE bFrameIndex;
    MMP_UBYTE bCompressIndex;
    MMP_ULONG dwMaxFrameSize;
    MMP_ULONG dwMaxPayloadSize;
} STILL_PROBE;
#else
typedef struct
{
    MMP_UBYTE bFormatIndex;
    MMP_UBYTE bFrameIndex;
    MMP_UBYTE bCompressIndex;
    MMP_UBYTE bMaxFrameSize[4];
    MMP_UBYTE bMaxPayloadSize[4];
} STILL_PROBE;
#endif

typedef __packed struct _SKYPE_UVC_PROBE
{
    MMP_UBYTE  bStreamType;
    MMP_USHORT wWidth;
    MMP_USHORT wHeight;
    MMP_ULONG  dwFrameInterval;
    MMP_ULONG  dwBitrate;
} SKYPE_UVC_PROBE;
typedef void* CMD_VALUE;
typedef struct _VC_CMD_CFG
{
    MMP_UBYTE  bCmdCap;//MMP_ULONG  bCmdCap;//Truman@120820 save 4 bytes each
    MMP_UBYTE  bInfoCap;
    MMP_USHORT bCmdLen ;
    //MMP_UBYTE  bInfoLen;
    /*If cmd > 4, the following data is a pointer*/
    CMD_VALUE  dwResVal;
    CMD_VALUE  dwDefVal;
    CMD_VALUE  dwMinVal;
    CMD_VALUE  dwMaxVal;
    CMD_VALUE  dwCurVal;
} VC_CMD_CFG;

#define UVC_EOF         (0x02) // end of frame
#define UVC_NOF         (0x00) // not end of frame
#define USBDMA_LIST_LEN            (0x0C)

#if STREAM_DMA_BLK_VER==3
    #if USB_UVC_BULK_EP
    #define MAX_LIST_NUM        (UVC_DMA_PAYLOAD_NUM*(TX_PER_FRAME+1))//one uvc header + one payload data(TX_PER_FRAME packet data)
    #else
    #define MAX_LIST_NUM        (UVC_DMA_PAYLOAD_NUM*2)//one uvc header + one payload data
    #endif
#else
    #define MAX_LIST_NUM        (8)//maximum 88
#endif

#define LIST_TXPKTRDY       (0x8000)
#define LIST_LAST           (0x4000)
#define LIST_INDEX          (0x2000)
#define LIST_FIXED_DATA     (0x1000)
#define UVC_HEADER_SIZE     (0x0C) // sizeof(UVC_VIDEO_PAYLOAD_HEADER), always is 0x0c

#define USB_DMA_DONE        (0x01)
#define USB_DMA_TXPKTRDY    (0x02)
#define BFH_FID     (0)
#define BFH_EOF     (1)
#define BFH_PTS     (2)
#define BFH_SCR     (3)
#define BFH_RES     (4)
#define BFH_STI     (5)
#define BFH_ERR     (6)
#define BFH_EOH     (7)
#define BFH_BIT(x)  ( 1 << x )

#define USE_COMMON_STALL          (1)  //Truman@120626 Use common stall or RX+stall
#define USE_REQUEST_FUNC_MAP      (1)//Truman@110816 Enable std_in functions table to reduce code size.
#define USE_COMMON_EP0_IN (1)//Truman@110815 Enable the common input data to reduce the code size.
#define USE_COMMON_EP0_OUT (1)//Truman@110817 Enable the common output data to reduce the code size.
#define USE_PACKED_PAYLOAD_HEADER (1)
#define USE_SIMPLIFIED_DESC       (1) //Truman@120711 Set 1 to avoid USBDescBufAddr to save code size.
#define USE_SIMPLIFIED_REQ (1) //Truman@110818 Enable simplified request to reduce code size.
#define USE_RESET_RANGE (1)//Truman@120719 Use common reset GET_MIN, GET_MAX, GET_RES
#define USE_COMMON_EP0_OUT        (1)
#define UVC_DIRECT_SEND           (1) //Truman@120718 3K buffer is not required if USB HW DESC list is there.
#define SUPPORT_SU   (0) //Select Unit

#if (SECS_VER > 0) && (SECS_VER < 20) //skype mode B, this should no longer needed and maintained.
    #define SKYPE_MODE_B        (1) // SECS 1.4 mode B
#else
    #define SKYPE_MODE_B        (0) // SECS 1.4 mode B
#endif

#define RES_REFERENCE_DESC        (1) // 1: use descriptor to get resolution information

#if USE_COMMON_EP0_IN
#define NULL_GET ((u_char*) 1) //get the data from the host but do not save the data
void usb_vc_get_ep0_data(MMP_UBYTE *const data);
void usb_vc_put_ep0_data(MMP_UBYTE in_phase, MMP_UBYTE size, MMP_UBYTE *const data);
void usb_vc_put_ep0_at_most(MMP_UBYTE in_phase,MMP_USHORT max_len,MMP_UBYTE *data);
#endif

#if USE_COMMON_STALL
    void usb_vc_stall_rx(const u_char uvc_error);
    void usb_vc_stall(const u_char uvc_error);
#else
    #define usb_uvc_stall(uvc_error)    UsbWriteEp0CSR(SET_EP0_SENDSTALL);   gbVCERRCode = uvc_error;
    #define usb_uvc_stall_rx(uvc_error)    UsbWriteEp0CSR(SET_EP0_SENDSTALL|SET_EP0_SERVICED_RXPKTRDY);   gbVCERRCode = uvc_error;
#endif

#if USE_SIMPLIFIED_DESC
#if SUPPORT_SWITCH_CAMERA
extern MMP_UBYTE *gpUsbDesc[3];
#else
extern MMP_UBYTE *gpUsbDesc[2];
#endif
#endif
typedef __packed struct
{
    MMP_ULONG dwNextDescAddr;
    MMP_ULONG dwPLDAddr;
    MMP_USHORT wPLDSize;
    MMP_USHORT wPara;
}USB_DMA_DESC;
#if USE_PACKED_PAYLOAD_HEADER
typedef __packed struct _UVC_VIDEO_PAYLOAD_HEADER
{
    MMP_UBYTE bHLE;
    MMP_UBYTE bBFH;
    MMP_ULONG bPTS;
    MMP_ULONG bSCR;
    MMP_USHORT bSOFNUM;
} UVC_VIDEO_PAYLOAD_HEADER;
#else
typedef struct _UVC_VIDEO_PAYLOAD_HEADER
{
    MMP_UBYTE bHLE;
    MMP_UBYTE bBFH;
    MMP_UBYTE bPTS[4] ;
    MMP_UBYTE bSCR[4] ;
    MMP_UBYTE bSOFNUM[2] ;
    
} UVC_VIDEO_PAYLOAD_HEADER;
#endif

// video control interface control selectors
#define  VC_CONTROL_UNDEFINED          0x00
#define  VC_VIDEO_POWER_MODE_CONTROL   0x01
#define  VC_REQUEST_ERROR_CODE_CONTROL 0x02
#define  VC_RESERVED                   0x03

// terminal control selectors
#define  TE_CONTROL_UNDEFINED          0x00

// selector unit control selectors
#define  SU_CONTROL_UNDEFINED          0x00
#define  SU_INPUT_SELECT_CONTROL       0x01

// camera terminal control selectors
#define  CT_CONTROL_UNDEFINED                  0x00
#define  CT_SCANNING_MODE_CONTROL              0x01
#define  CT_AE_MODE_CONTROL                    0x02
#define  CT_AE_PRIORITY_CONTROL                0x03
#define  CT_EXPOSURE_TIME_ABSOLUTE_CONTROL     0x04
#define  CT_EXPOSURE_TIME_RELATIVE_CONTROL     0x05
#define  CT_FOCUS_ABSOLUTE_CONTROL             0x06
#define  CT_FOCUS_RELATIVE_CONTROL             0x07
#define  CT_FOCUS_AUTO_CONTROL                 0x08
#define  CT_IRIS_ABSOLUTE_CONTROL              0x09
#define  CT_IRIS_RELATIVE_CONTROL              0x0A
#define  CT_ZOOM_ABSOLUTE_CONTROL              0x0B
#define  CT_ZOOM_RELATIVE_CONTROL              0x0C
#define  CT_PANTILT_ABSOLUTE_CONTROL           0x0D
#define  CT_PANTILT_RELATIVE_CONTROL           0x0E
#define  CT_ROLL_ABSOLUTE_CONTROL              0x0F
#define  CT_ROLL_RELATIVE_CONTROL              0x10
#define  CT_PRIVACY_CONTROL                    0x11
//UVC 1.5
#define  CT_FOCUS_SIMPLE_CONTROL               0x12
#define  CT_WINDOW                             0x13
#define  CT_REGION_OF_INTEREST                 0x14

// CT support 
#define CT_CS_VAL (  ( 1 << (CT_AE_MODE_CONTROL - 1    )  ) \
                     ( 1 << (CT_AE_PRIORITY_CONTROL - 1)  ) \
                     ( 1 << (CT_EXPOSURE_TIME_ABSOLUTE_CONTROL - 1)  ) \
                  ) 

// processing unit control selectors
#define  PU_CONTROL_UNDEFINED                      0x00
#define  PU_BACKLIGHT_COMPENSATION_CONTROL         0x01
#define  PU_BRIGHTNESS_CONTROL                     0x02
#define  PU_CONTRAST_CONTROL                       0x03
#define  PU_GAIN_CONTROL                           0x04
#define  PU_POWER_LINE_FREQUENCY_CONTROL           0x05
#define  PU_HUE_CONTROL                            0x06
#define  PU_SATURATION_CONTROL                     0x07
#define  PU_SHARPNESS_CONTROL                      0x08
#define  PU_GAMMA_CONTROL                          0x09
#define  PU_WHITE_BALANCE_TEMPERATURE_CONTROL      0x0A
#define  PU_WHITE_BALANCE_TEMPERATURE_AUTO_CONTROL 0x0B
#define  PU_WHITE_BALANCE_COMPONENT_CONTROL        0x0C
#define  PU_WHITE_BALANCE_COMPONENT_AUTO_CONTROL   0x0D
#define  PU_DIGITAL_MULTIPLIER_CONTROL             0x0E
#define  PU_DIGITAL_MULTIPLIER_LIMIT_CONTROL       0x0F
#define  PU_HUE_AUTO_CONTROL                       0x10
#define  PU_ANALOG_VIDEO_STANDARD_CONTROL          0x11
#define  PU_ANALOG_LOCK_STATUS_CONTROL             0x12

// extension unit control selectors
#define  XU_CONTROL_UNDEFINED                      0x00
#if USB_MJPEGH264_STREAM
// H264 XU for LGT begin
#define  XU_ENCODER_VIDEO_FORMAT_CONTROL           0x01
    #define XU_ENCODER_VIDEO_FORMAT_LEN            (10)
#define  XU_ENCODER_CONFIGURATION_CONTROL          0x02
    #define XU_ENCODER_CONFIGURATION_LEN           (25) 
#define  XU_RATE_CONTROL                           0x03
    #define XU_RATE_CONTROL_LEN                    (8)
#define  XU_FRAME_TYPE_CONTROL                     0x04
    #define XU_FRAME_TYPE_LEN                      (1)
#define  XU_CAMERA_DELAY_CONTROL                   0x05
    #define XU_CAMERA_DELAY_LEN                    (2)
#define  XU_FILTER_CONTROL                         0x06            
    #define XU_FILTER_LEN                          (2)
#endif

// video streaming interface control selectors
// Interface control request ( wValue )
#define  VS_CONTROL_UNDEFINED                0x00
#define  VS_PROBE_CONTROL                    0x01
#define  VS_COMMIT_CONTROL                   0x02
#define  VS_STILL_PROBE_CONTROL              0x03
#define  VS_STILL_COMMIT_CONTROL             0x04
#define  VS_STILL_IMAGE_TRIGGER_CONTROL      0x05
#define  VS_STREAM_ERROR_CODE_CONTROL        0x06
#define  VS_GENERATE_KEY_FRAME_CONTROL       0x07
#define  VS_UPDATE_FRAME_SEGMENT_CONTROL     0x08
#define  VS_SYNCH_DELAY_CONTROL              0x09

//VC descriptor type 
#define CS_UNDEFINED                         0x20
#define CS_DEVICE                            0x21
#define CS_CONFIGURATION                     0x22
#define CS_STRING                            0x23
#define CS_INTERFACE                         0x24
#define CS_ENDPOINT                          0x25

//Video Interface Class Code
#define SC_UNDEFINED                    0x00
#define SC_VIDEOCONTROL                 0x01
#define SC_VIDEOSTREAMING               0x02
#define SC_VIDEO_INTERFACE_COLLECTION   0x03

//VC Interface descriptor subtype 
#define VC_DESCRIPTOR_UNDEFINED         0x00
#define VC_HEADER                       0x01
#define VC_INPUT_TERMINAL               0x02
#define VC_OUTPUT_TERMINAL              0x03
#define VC_SELECTOR_UNIT                0x04
#define VC_PROCESSING_UNIT              0x05
#define VC_EXTENSION_UNIT               0x06

//VS Interface descriptor type
#define VS_UNDEFINED                    0x00
#define VS_INPUT_HEADER                 0x01
#define VS_OUTPUT_HEADER                0x02
#define VS_STILL_IMAGE_FRAME            0x03
#define VS_FORMAT_UNCOMPRESSED          0x04
#define VS_FRAME_UNCOMPRESSED           0x05
#define VS_FORMAT_MJPEG                 0x06
#define VS_FRAME_MJPEG                  0x07
#define VS_FORMAT_MPEG2TS               0x0A
#define VS_FORMAT_DV                    0x0C
#define VS_COLORFORMAT                  0x0D
#define VS_FORMAT_FRAME_BASED           0x10
#define VS_FRAME_FRAME_BASED            0x11
#define VS_FORMAT_STREAM_BASED          0x12

//endpoint descriptor subtype
#define EP_UNDEFINED                    0x00
#define EP_GENERAL                      0x01
#define EP_ENDPOINT                     0x02
#define EP_INTERRUPT                    0x03

// capability info 
#define  INFO_GET_SUPPORT                0x01
#define  INFO_SET_SUPPORT                0x02
#define  INFO_AUTO_MODE_SUPPORT          0x04
#define  INFO_AUTOUPDATE_CONTROL_SUPPORT 0x08
#define  INFO_ASYNC_CONTROL_SUPPORT      0x10

// banding filter
#define BAND_DISABLE             0x00
#define BAND_50HZ                0x01
#define BAND_60HZ                0x02
#define BAND_AUTO_LE             0x03   //LE:LIMITEXPO
#define BAND_50HZ_LE             0x04
#define BAND_60HZ_LE             0x05
#if (CUSTOMER == ANW)
#define BAND_MAX                 BAND_60HZ_LE
#else
#define BAND_MAX                 BAND_60HZ
#endif

//UVC unit ID
#define UVC_EU0_ID      (0x07)//(0x05) //Truman@120710 reserved for SECS 2.1
#define UVC_EU1_ID      (0x06)
#define UVC_EU2_ID      (0x02)
#define UVC_EU3_ID      (0x08)
#define UVC_OT_ID       (0x04)
#define UVC_PU_ID       (0x03)
#define UVC_IT_ID       (0x01)

// total numbers of video frames
#if USB_UVC_1080P_EN
#define NUM_H264_VIDEO (11)
#else
#define NUM_H264_VIDEO (10)
#endif

// number of YUY2 frame descriptor
#if (CUSTOMER == ASU)
    #define NUM_D1_YUY2_VIDEO (1)//(0x04)//(6)//ASU 1st interface is HD (ov9726)
#elif (CUSTOMER == QDR)
	#define NUM_D1_YUY2_VIDEO (0x09)
#else//general case
#if (USB_UVC_5M_EN) || (USB_UVC_1080P_YUY2_EN)
    #if (USB_UVC_1080P_YUY2_EN)
    #define NUM_D1_YUY2_VIDEO (0x0D)
    #endif
    #if (USB_UVC_5M_EN)
    #undef NUM_D1_YUY2_VIDEO
    #define NUM_D1_YUY2_VIDEO (0x0F)
    #endif
#if (USB_UVC_5M_EN) && (USB_UVC_1080P_YUY2_EN)
    #undef NUM_D1_YUY2_VIDEO
    #define NUM_D1_YUY2_VIDEO (0x11)
    #endif
#else
    #define NUM_D1_YUY2_VIDEO (0x04)//(6)
#endif
#endif
#define NUM_D2_YUY2_VIDEO     (1)//(4)//(6)

// number of MJPEG frame descriptor
#if (CUSTOMER == ASU)
    #define NUM_D1_MJPG_VIDEO (0x02)//(0x04)//ASU 1st interface is HD (ov9726)
#elif (CUSTOMER == QDR)
    #define NUM_D1_MJPG_VIDEO (0x0C)
    #if BIND_SENSOR_AR0330
    #undef  NUM_D1_MJPG_VIDEO
    #define NUM_D1_MJPG_VIDEO (0x0D)
    #elif BIND_SENSOR_AR0832E
    #undef  NUM_D1_MJPG_VIDEO
    #define NUM_D1_MJPG_VIDEO (0x06)
    #endif
#else//general case
    #if (USB_UVC_5M_EN) && (USB_UVC_1080P_MJPEG_EN)
    #define NUM_D1_MJPG_VIDEO (0x12)
    #elif (USB_UVC_5M_EN)
    #define NUM_D1_MJPG_VIDEO (0x11)
    #elif (USB_UVC_1080P_MJPEG_EN)
    #define NUM_D1_MJPG_VIDEO (0x0E)
#else
    #define NUM_D1_MJPG_VIDEO (0x0D)
#endif
#endif

#if (SUPPORT_DUAL_DEVICE) || (SUPPORT_SWITCH_CAMERA)
#define NUM_D2_MJPG_VIDEO (0x05)//(0x12)//(9)//ASU 2nd interface is 5M (ov5647)
#else
#define NUM_D2_MJPG_VIDEO (0x0D)
#endif

#define STILL_ENABLE        (USING_STILL_METHOD_1 != 1)
// total numbers of still frames
#define NUM_YUY2_STILL 3
#if BIND_SENSOR_AR0330
#define NUM_MJPG_STILL 9
#elif BIND_SENSOR_AR0832E
#define NUM_MJPG_STILL 6
#else
#define NUM_MJPG_STILL 7
#endif

//still capture
#define STILL_IMAGE_WAITING 0
#define STILL_IMAGE_TRIGGER 1
#define STILL_IMAGE_PREVIEW_STOP 2
#define STILL_IMAGE_SENDING 3
#define STILL_IMAGE_FINISH 4

#define PAYLOAD_FORMAT_ID_YUY2 0	//YUY2	skype mode B
#define PAYLOAD_FORMAT_ID_MJPG 1	//MJPG	LGT mode
#define PAYLOAD_FORMAT_ID_UVC11_H264 2	//USBIF UVC	mode

//#define NUM_FRAME_DESC 21
#define SKYPE_YUY2_IDX 0 //mode B
#define SKYPE_MJPEG_IDX (SKYPE_MODE_B * 6) //LGT mode
#define SKYPE_UVC11_H264_IDX (SKYPE_MJPEG_IDX + (USB_MJPEGH264_STREAM)*10 + (USB_UVC_1080P_MJPEG_EN && USB_MJPEGH264_STREAM) * 2)
#define NUM_FRAME_DESC (SKYPE_UVC11_H264_IDX + SKYPE_UVC_H264 * 3)

#define I2C_MODE_1A1D 1
#define I2C_MODE_2A1D 0

#define EU_ISP_CMD_OK           0x00
#define EU_ISP_CMD_OUT_OF_RANGE 0x01
#define EU_ISP_CMD_NOT_SUPPORT  0x80
#define EU_ISP_CMD_FAILED       0x81
#define XU_MMP_CMD_OK           0x00
#define XU_MMP_DOWNLOADING      0x80 //downloading the firmware
#define XU_MMP_BURNING          0x81
#define XU_MMP_BURNING_ERROR    0x82

//Truman@120906 The host tool from Andy uses packed.
//2 bytes padding will be added and would not cause error if __packed is not added.
__packed typedef struct _AitH264FrameDesc
{
	MMP_USHORT	Length;		//Length of this descriptor
	MMP_UBYTE	PayloadFmt;	//Payload format
	MMP_UBYTE	Index;		//Frame index
	MMP_USHORT	Width;		//H264 Frame width
	MMP_USHORT	Height;		//H264 Frame height
	MMP_USHORT	LocalWidth; //Local preview width
	MMP_USHORT	LocalHeight;//Local preview height
	MMP_ULONG	MaxBitrate;	//Max bitrate
	MMP_ULONG	MinBitrate; //Min bitrate
	MMP_UBYTE	MaxFps;		//Max FPS
	MMP_UBYTE	MinFps;		//Min FPS
}AitH264FrameDesc;

typedef struct _AitH264FrameList
{
	MMP_USHORT Length;						//Length if this descriptor
	MMP_USHORT NumFrameDesc;				//Number of frame descriptor in this list
	#if NUM_FRAME_DESC > 0
	AitH264FrameDesc FrameList[NUM_FRAME_DESC];		//frame descriptor
	#endif
}AitH264FrameList;	

typedef struct _DataExchangeParam
{
	MMP_USHORT	id;			//command ID
	MMP_ULONG	data_len;	//transfer lens
	MMP_ULONG	cur_offset;	//current read/write offset
	MMP_BYTE	*ptr;		//data buffer
}DataExchangeParam;

#if XU_FRAMEBASE_CAPTURE
typedef enum CAPTURE_FORMAT {//CF stands for Capture Format
    CF_YUY2 = PIPE_CFG_YUY2,
    CF_H264 = PIPE_CFG_H264,
    CF_MJPEG = PIPE_CFG_MJPEG
} CAPTURE_FORMAT;

typedef struct _FRAMEBASE_CAPTURE {
    CAPTURE_FORMAT format;//Ex_format, its original settings are 1:H264 2:MJPG 3:YUY2
    CAPTURE_FORMAT orgFormat;
    MMP_UBYTE orgFramerate;     //Save YUY2/MJPG framerate before capture
    MMP_UBYTE orgMJPG_Quality;  //Save MJPG quality before capture
    MMP_UBYTE CapResol;	//Capture resolution
    MMP_BOOL JPEG;		//Capture_MJPG;
    MMP_BOOL SECS;		//Capture_SECS
} FRAMEBASE_CAPTURE;
extern FRAMEBASE_CAPTURE gFramebaseCap;
#endif

#define UVC_IT_BITMAP       (UVC_D1_IT_BITMAP | UVC_D2_IT_BITMAP)
#define UVC_PU_BITMAP       (UVC_D1_PU_BITMAP | UVC_D2_PU_BITMAP)

void usb_vc_preview_enable(void);
void usb_vc_preview_disable(void);

#define UVC_SEND_IMG_RET_CONTINUE 0x00
#define UVC_SEND_IMG_RET_END_FRAME 0x01
MMP_UBYTE usb_vc_send_image(MMP_UBYTE);
#if (STREAM_DMA_BLK_VER == 3)
//void usb_vc_send_payload(MMP_ULONG cur_pt,MMP_USHORT xsize,MMP_USHORT endbit,MMP_UBYTE sti_mode);
//MMP_ULONG usb_vc_move_packets(STREAM_DMA_BLK *dma_blk,MMP_UBYTE sti_mode,MMP_UBYTE *pad_app3_header,MMP_USHORT *sent_size);
//MMP_ULONG usb_vc_fill_header_packet(MMP_UBYTE endbit,MMP_UBYTE sti_mode);
#else
void usb_vc_send_packet(MMP_ULONG cur_pt,MMP_USHORT xsize) ;
MMP_ULONG usb_vc_move_packet(STREAM_DMA_BLK *dma_blk,MMP_UBYTE endbit,MMP_UBYTE sti_mode,MMP_UBYTE *pad_app3_header,MMP_ULONG *sent_size);
MMP_ULONG usb_vc_fill_empty_packet(STREAM_DMA_BLK *dma_blk,MMP_UBYTE endbit,MMP_UBYTE sti_mode);
#endif

MMP_ERR   usb_vc_wait_dma_done(MMPF_DMA_R_ID DMA_ID,MMP_ULONG timeout); // timeout is loop count  
void usb_vc_set_still_commit(STILL_PROBE* pSCIN);
void usb_vc_get_still_probe(MMP_UBYTE FormatIndex,MMP_UBYTE FrameIndex ,STILL_PROBE* pSP);
void usb_vc_set_still_probe(MMP_UBYTE FormatIndex,MMP_UBYTE FrameIndex);
void usb_vc_set_still_resolution(MMP_UBYTE FormatIndex,MMP_UBYTE FrameIndex);
void usb_vc_init_sensor_param(void);
MMP_UBYTE SetH264Mode(MMP_UBYTE Mode);
void usb_uvc_set_h264_mode(MMP_UBYTE FormatIndex,MMP_UBYTE FrameIndex);
void usb_uvc_start_preview(void);
void usb_uvc_suspend(void);
void usb_uvc_config_pipe(const STREAM_CFG * stream_cfg,PCAM_USB_VIDEO_RES pipe0_res,PCAM_USB_VIDEO_RES pipe1_res,PCAM_USB_VIDEO_RES pipe2_res);

MMPF_FCTL_LINK *usb_uvc_get_preview_fctl_link (MMP_UBYTE ubPipe);
MMP_BOOL usb_uvc_config_stream(void);
void usb_uvc_init_stream(MMP_BOOL mjpeg_stream);
void usb_uvc_start_stream(MMP_BOOL mjpeg_stream,const STREAM_CFG *stream_cfg,MMP_ULONG res);
void usb_fill_payload_header (MMP_UBYTE *frame_ptr, MMP_ULONG framelength, MMP_ULONG frameseq,
                                MMP_ULONG flag, MMP_ULONG timestamp, MMP_USHORT w, MMP_USHORT h,
                                MMP_USHORT framerate, MMP_UBYTE format_type, MMP_UBYTE ubPipe);
//void usb_uvc_fill_payload_header(MMP_UBYTE *frame_ptr,MMP_ULONG framelength,MMP_ULONG frameseq,MMP_ULONG flag,MMP_ULONG timestamp,MMP_USHORT w,MMP_USHORT h,MMP_USHORT type,MMP_USHORT framerate);
//void usb_skype_fill_payload_header(MMP_UBYTE *frame_ptr,MMP_ULONG framelength,MMP_ULONG frameseq,MMP_ULONG flag,MMP_ULONG timestamp,MMP_USHORT w,MMP_USHORT h,MMP_USHORT type,MMP_USHORT framerate);
//void usb_frameh264_fill_payload_header(MMP_UBYTE *frame_ptr,MMP_ULONG framelength,MMP_ULONG frameseq,MMP_ULONG flag,MMP_ULONG timestamp,MMP_USHORT w,MMP_USHORT h,MMP_USHORT type,MMP_USHORT framerate);
//void usb_skype_uvc_fill_payload_header(MMP_UBYTE *frame_ptr,MMP_ULONG framelength,MMP_ULONG frameseq,MMP_ULONG flag,MMP_ULONG timestamp,MMP_USHORT w,MMP_USHORT h,MMP_USHORT type,MMP_USHORT framerate);
MMP_UBYTE *usb_uvc_search_app3_insert_addr(MMP_UBYTE *jpeg,MMP_ULONG len,MMP_USHORT tag);

//void usb_dma_setlist(MMP_USHORT usSize, MMP_ULONG ulAddr, MMP_ULONG ulFbAddr, MMP_USHORT usPara);
//void usb_dma_en(MMP_ULONG ulAddr,MMP_UBYTE ubEnableInt, MMP_UBYTE ubEndpoint);
void usb_dma_setlist(MMP_USHORT usSize, MMP_ULONG ulFbAddr, MMP_USHORT usPara);
void usb_dma_en(MMP_UBYTE ubEnableInt, MMP_UBYTE ubEndpoint);
void usb_dma_reset(void);

void uvc_init(MMP_BOOL reset_val);
void usb_vc_req_proc(MMP_UBYTE req);
void usb_vc_update_automode(void);
void usb_vc_out_data(void);
void usb_vc_capture_trigger(MMP_UBYTE pressed);
#if SUPPORT_HID_FUNC
//void usb_hid_trigger(MMP_UBYTE event);
void usb_hid_trigger(MMP_UBYTE event, MMP_UBYTE modifier );
#endif
void usb_uvc_stop_preview(void);
void usb_vc_update_async_mode(MMP_UBYTE bOriginator,MMP_UBYTE bSelector,MMP_UBYTE bAttribute,void *bValue,MMP_UBYTE bValUnit);
MMP_ULONG usb_vc_take_raw_picture(MMP_UBYTE raw_mode, MMP_ULONG srcAddr,MMP_ULONG dstAddr);
volatile MMP_UBYTE *usb_vc_fill_header(volatile MMP_UBYTE *ptr,MMP_UBYTE bfh,MMP_ULONG pts,MMP_ULONG stc,MMP_USHORT sofnum);
H264_ENCODER_VFC_CFG *usb_get_uvc_h264_cfg(void);
void USB_SET_FIFO_DATA(MMP_UBYTE req,MMP_UBYTE ubEpNum,MMP_UBYTE unit,MMP_UBYTE val);
void USB_SET_FIFO_DATA_ONE(MMP_UBYTE req,MMP_UBYTE ubEpNum,MMP_UBYTE unit);
void usb_vc_cmd_cfg(MMP_UBYTE req,VC_CMD_CFG *cfg, CMD_VALUE const cur_val);

// mmpf_usb_task.c32
void MMPF_USB_WaitDMA(void);
void MMPF_USB_ReleaseDMA(void);
void USB_GET_FIFO_DATA(MMP_UBYTE ubEpNum,MMP_USHORT fifo_len,MMP_UBYTE *buf,MMP_USHORT buf_len) ;

RES_TYPE_LIST mjpg_frame_index_to_pcam(MMP_UBYTE res_index);
RES_TYPE_LIST yuy2_frame_index_to_pcam(MMP_UBYTE res_index);
#endif
